package com.artPreter.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.artPreter.mapper.UserMapper;
import com.artPreter.pojo.User;
import com.artPreter.service.UserService;
import com.artPreter.utils.CsvUtils;
import com.artPreter.utils.Md5Util;
import com.artPreter.utils.ThreadLocalUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUserName(String username) {

        User u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
        //加密
        String md5String = Md5Util.getMD5String(password);
        String uuid = UUID.randomUUID().toString();

        //添加
        userMapper.add(username,md5String,uuid);

    }

    @Override
    public void update(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uuid",user.getUuid());
        user.setUpdateTime(LocalDateTime.now());
        this.baseMapper.update(user,userQueryWrapper);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }

    @Override
    public IPage<User> getPage(Integer currentPage, Integer size, User user) {
        IPage<User> userIPage = new Page<>(currentPage,size);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        // 部门
        if(user.getDepart()!=null&&!user.getDepart().isEmpty()){
            userQueryWrapper.eq("depart",user.getDepart());
        }
        // 姓名
        if(user.getNickname()!=null&&!user.getNickname().isEmpty()){
            userQueryWrapper.like("nickname",user.getNickname());
        }
        userIPage = this.baseMapper.selectPage(userIPage,userQueryWrapper);
        return userIPage;
    }


    @Override
    public void add(User user) {
        //加密
        String md5String = Md5Util.getMD5String("creater88872333");
        md5String = Md5Util.getMD5String(md5String);
        String uuid = UUID.randomUUID().toString();
        user.setPassword(md5String);
        user.setUuid(uuid);
        this.baseMapper.insert(user);
    }

    @Override
    public void edit(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uuid",user.getUuid());
        this.baseMapper.update(user,userQueryWrapper);
    }

    @Override
    public void delete(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uuid",user.getUuid());
        List<User> userList = this.baseMapper.selectList(userQueryWrapper);
        if(userList.size()>0) {
            User updateData = new User(userList.get(0),2);
            this.baseMapper.update(updateData,userQueryWrapper);
        }
    }


    public String createpath() {
        String separator = File.separator;

        // 服务器nginx位置
        String serverPath = "D:\\java\\uploads";

        // 年月日文件夹
        LocalDateTime now = LocalDateTime.now();
        String dayStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String[] dayArr = dayStr.split("-");
        String dateDir = separator + dayArr[0] + separator + dayArr[1] + separator + dayArr[2];
        String path = serverPath + dateDir;

        String year = dayArr[0];
        String month = dayArr[1];

        String yearDir = serverPath + File.separator + year;
        File yearFile = new File(yearDir);
        if (!yearFile.exists()) {
            yearFile.mkdirs();
        }

        String monthDir = yearDir + File.separator + month;
        File monthFile = new File(monthDir);
        if (!monthFile.exists()) {
            monthFile.mkdirs();
        }

        String dayDir = path;
        File dayFile = new File(dayDir);
        if (!dayFile.exists()) {
            dayFile.mkdirs();
        }
        String result = serverPath + separator + year + separator + month + separator + dayArr[2] + separator;
        return result;
    }
}
